<script setup lang="tsx">
import Write from './components/SysMachineReportWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { useBaseDetailView } from '@/base/views/BaseDetailView'
import { sysMachineReportAddApi, sysMachineReportDetailApi, sysMachineReportUpdateApi } from '@/api/system/monitor/SysMachineReport'
import { SysMachineReportDetailVo, SysMachineReportEditQo } from '@/api/system/monitor/SysMachineReport/types'

const writeRef = ref<ComponentRef<typeof Write>>()
const detailView = useBaseDetailView<SysMachineReportDetailVo, SysMachineReportEditQo>(writeRef, 'SysMachineReport')
const { title, loading, baseLoadDetail, baseSave, currentRow, id, back } = detailView

const { query } = useRoute()
const editType = query.type as string

const save = async () => {
  await baseSave(
    async (formData) => {
      await sysMachineReportUpdateApi(formData)
    },
    async (formData) => {
      await sysMachineReportAddApi(formData)
    }
  )
}

const loadDetail = async () => {
  await baseLoadDetail(sysMachineReportDetailApi)
}

loadDetail()
</script>

<template>
  <ContentDetailWrap :title="title">
    <Write ref="writeRef" :current-row="currentRow" :edit-type="editType" :edit-id="id" />

    <template #header>
      <BaseButton @click="back">返回</BaseButton>
      <BaseButton type="primary" :loading="loading" @click="save" permission="'save'">
        保存
      </BaseButton>
    </template>
  </ContentDetailWrap>
</template>
